package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.enums.ResponseCodeEnum;
import com.example.common.utils.ApiResult;
import com.example.model.entity.TSource;
import com.example.model.query.TSourceQuery;
import com.example.service.TSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  前端控制器
 *
 * @author zhangr
 * @Description Created on 2020-12-11
 */
@Controller
@RequestMapping("tSource")
@RestController
@CrossOrigin
@Api(value = "告警类型管理",tags={"告警类型管理接口"})
public class TSourceController {

    @Autowired
    private TSourceService sourceService;

    /**
     * 分页查询列表
     */
    @GetMapping("/page")
    public ApiResult<TSourceQuery> pageByQuery(TSourceQuery query) {
        try {
            return sourceService.pageUserVoByQuery(query);
        }   catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_PAGE_AD_FALSE);
        }
    }


    /**
     * 获取所有来源
     * @return
     */
    @ApiOperation(value = "获取所有来源", notes = "查询获取所有来源", httpMethod = "GET")
    @GetMapping("/getList")
    public ApiResult<List<TSource>> getList(){
        try {
            return sourceService.getList();
        }catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_PAGE_CUSTOMER_FALSE);
        }
    }
    /*
     * 新增
     *
     */
    @ApiOperation(value = "新增区域", notes = "新增区域", httpMethod = "POST")
    @PostMapping("/save")
    public ApiResult<Boolean> save(@RequestBody TSource area){
        try{
            return sourceService.saveT(area);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_SAVE_AD_FALSE);
        }
    }

    /*
     * 修改
     *
     */
    @ApiOperation(value = "修改区域", notes = "修改区域", httpMethod = "POST")
    @PostMapping("/modify")
    public ApiResult<Boolean> modify(@RequestBody TSource area){
        try{
            return new ApiResult<>(sourceService.updateById(area));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_SAVE_AD_FALSE);
        }
    }

    /*
     * 删除
     *
     */
    @GetMapping("/del/{id}")
    public ApiResult<Boolean> del(@PathVariable("id") String id){
        try {
            QueryWrapper<TSource> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TSource::getSourceId,id);
            TSource obj =new TSource();
            obj.setIsValid(0L);
            return new ApiResult<>(sourceService.update(obj,queryWrapper));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_GET_FALSE);
        }
    }
}
